
/* GCSx
** SCRIPTEDITOR.CPP
**
** Script editing window
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#include "all.h"

ScriptEditor::ScriptEditor(list<string>* scriptData, ScriptEdit* myScript) throw_File : EditBox(scriptData, FONT_MONO) { start_func
    wordwrap = myScript->getType() == Script::SCRIPT_NOTE ? 1 : 0;
    autoIndent = myScript->getType() == Script::SCRIPT_NOTE ? 0 : 1;
    script = myScript;
    script->markLock();
}

ScriptEditor::~ScriptEditor() { start_func
    if (script) script->markUnlock();
}

void ScriptEditor::contentModifiedPre(EditBox::ContentChangeType type, int firstRow, int numRows) { start_func
    if (script) {
        assert((type == EDITBOX_MODIFY_LINE) || (type == EDITBOX_INSERT_LINES) || (type == EDITBOX_REMOVE_LINES) || (type == EDITBOX_MODIFY_DONE));
        ScriptEdit::ContentChangeType newType = ScriptEdit::SCRIPT_MODIFY_DONE;
        if (type == EDITBOX_MODIFY_LINE) newType = ScriptEdit::SCRIPT_MODIFY_LINE;
        else if (type == EDITBOX_INSERT_LINES) newType = ScriptEdit::SCRIPT_INSERT_LINES;
        else if (type == EDITBOX_REMOVE_LINES) newType = ScriptEdit::SCRIPT_REMOVE_LINES;
        script->codeModifiedPre(newType, firstRow, numRows, this, myFrame);
    }
}

void ScriptEditor::contentModifiedPost(EditBox::ContentChangeType type, int firstRow, int numRows) { start_func
    if (script) {
        assert((type == EDITBOX_MODIFY_LINE) || (type == EDITBOX_INSERT_LINES) || (type == EDITBOX_REMOVE_LINES) || (type == EDITBOX_MODIFY_DONE));
        ScriptEdit::ContentChangeType newType = ScriptEdit::SCRIPT_MODIFY_DONE;
        if (type == EDITBOX_MODIFY_LINE) newType = ScriptEdit::SCRIPT_MODIFY_LINE;
        else if (type == EDITBOX_INSERT_LINES) newType = ScriptEdit::SCRIPT_INSERT_LINES;
        else if (type == EDITBOX_REMOVE_LINES) newType = ScriptEdit::SCRIPT_REMOVE_LINES;
        script->codeModifiedPost(newType, firstRow, numRows, this, this);
    }
}

void ScriptEditor::updateTitlebar() { start_func
    if ((myFrame) && (script)) {
        myFrame->setTitle(formatString("%s : %s", script->getWorld()->getTitle().c_str(), script->getName().c_str()));
    }
}

int ScriptEditor::event(int hasFocus, const SDL_Event* event) { start_func
    ObjChange* obj;

    switch (event->type) {
        case SDL_COMMAND:            
            if (script->getWorldEdit()->commandEvent(event->user.code)) return 1;
            break;
    
        case SDL_OBJECTCHANGE:
            obj = (ObjChange*)event->user.data1;
            if ((event->user.code & OBJ_SCRIPT) && (obj->obj == script)) {
                if (event->user.code & OBJMOD_DELETE) {
                    script = NULL;
                    closeWindow();
                }
                if (event->user.code & OBJMOD_NAME) {
                    updateTitlebar();
                }
                if (event->user.code & OBJMOD_LINE) {
                    contentModify(obj->info1, obj->info2);
                }
                if (event->user.code & OBJMOD_INSERT) {
                    contentInsert(obj->info1, obj->info2);
                }
                if (event->user.code & OBJMOD_REMOVE) {
                    contentDelete(obj->info1, obj->info2);
                }
            }
            if ((event->user.code & OBJ_WORLD) && (obj->obj == script->getWorld())) {
                if (event->user.code & OBJMOD_DELETE) {
                    script = NULL;
                    closeWindow();
                }
                if (event->user.code & OBJMOD_NAME) {
                    updateTitlebar();
                }
            }
            return 1;
    }

    return EditBox::event(hasFocus, event);
}

Window::CommandSupport ScriptEditor::supportsCommand(int code) const { start_func
    Window::CommandSupport worldCmd = script->getWorldEdit()->supportsCommand(code);
    if (worldCmd != Window::COMMAND_HIDE) return worldCmd;
    return EditBox::supportsCommand(code);
}

